package com.dartfar.permission.interceptor;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.StrutsStatics;

import com.dartfar.base.model.Person;
import com.dartfar.base.model.Role;
import com.dartfar.base.model.T_User;
import com.dartfar.base.service.RoleService;
import com.dartfar.menu.model.Menu;
import com.dartfar.menu.service.MenuService;
import com.dartfar.util.EntityUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;


/**
 * 访问权限拦截器
 * @author 王琦
 *
 */
public class CheckResourceInterceptor implements  Interceptor {

	private static final long serialVersionUID = 1L;
	
	private static Logger logger = Logger.getLogger(CheckResourceInterceptor.class);
		
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		ActionContext actionContext = invocation.getInvocationContext();     
		Map<String,Object> session = actionContext.getSession();
		HttpServletRequest request= (HttpServletRequest) actionContext.get(StrutsStatics.HTTP_REQUEST);
		
		//访问地址
		String actionName = actionContext.getName();
		String methodName = invocation.getProxy().getMethod();
		
		String target = actionName + "!" + methodName;
		//获取人员对象
		Person person = (Person)session.get("person");
		//如果对象不为NULL
		if(EntityUtils.isNotNull(person)){
			//是否有权限的标示
        	boolean hasAuthority = false;
        	
        	//如果methodName 等于 view
        	if(methodName.equalsIgnoreCase("view")){
        		//则拥有页面访问权限
				hasAuthority = true;
    		}else{
    	     	
        		//获取拥有的资源列表
            	List<Menu> menus = (List<Menu>) session.get("subMenus");
        		//遍历资源
        		for(Iterator<Menu> menulist = menus.iterator(); menulist.hasNext();){
        			//实体化资源
        			Menu menu = menulist.next();
        			//拥有地址与访问地址一致
        			if(menu.getTarget().equalsIgnoreCase(target)){
        				//则拥有页面访问权限
        				hasAuthority = true;
        				//把菜单ID写入actionContext
        				session.put("wid", menu.getId());
        				break;
        			}
        		}
    		}
   

        	//判断是否拥有此地址访问的权限
        	if(hasAuthority){
        		logger.info("拥有访问权限.");
        		return invocation.invoke();
        	}else{
        		logger.info("没有访问权限.");
        		try{
        			session.clear();
            		request.setAttribute("errorMessage", "您没有查看权限!");
        		}catch (Exception e) {
					// TODO: handle exception
        			e.printStackTrace();
				}
        		return "error";
        	}
        }else{
        	request.setAttribute("errorMessage", "登录超时，请重新登录!");
			return "error";
        }
	}	
	
}
